<?php

declare(strict_types=1);

namespace App\Controller;

use App\Dao\CityIpDao;
use App\Request\LoginRequest;
use App\Services\LoginService;
use App\Utils\FunUtil;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;

class LoginController extends BaseController
{
    #[Inject]
    protected LoginService $loginService;

    public function login(LoginRequest $request)
    {
        $params = $this->request->post();

        $account = $params['account'] ?? '';
        $code = $params['code'] ?? '';// 验证码
        $type = $params['type'] ?? 1; // 登录方式

        // 验证参数
        $request->execute($params, $type);

        $inviteUid = $params['inviteUid'] ?? 0; // 邀请码
        $oaid = (empty($params['oaid']) || ($params['oaid'] == '00000000-0000-0000-0000-000000000000') || ($params['oaid'] == '__OAID__')) ? null : $params['oaid'];

        $imei = (empty($params['imei']) || ($params['imei'] == '__IMEI__')) ? null : $params['imei'];

        $mapLocation = json_decode($params['mapLocation'] ?? '{}', true);
        $country = $mapLocation['country'] ?? '未知';
        $province = $mapLocation['province'] ?? '';
        $city = $mapLocation['city'] ?? '';
        $lng = 0;
        $lat = 0;
        $register_ip = FunUtil::getClientIp();
        if (empty($province)) {
            $cityIpDao = new CityIpDao();
            $ipcache = $cityIpDao->getProvinceCity($register_ip);
            $country = $ipcache['country'] ?? '未知';
            $province = $ipcache["province"] ?? "火星";
            $city = $ipcache['city'];
            $lng = $ipcache['lng'];
            $lat = $ipcache['lat'];
        }

        $this->loginService->banLogin($imei, $account, $province, $inviteUid);

        $commonParams = [
            'imei' => $imei,
            'oaid' => $oaid,
            'country' => $country,
            'province' => $province,
            'city' => $city,
            'lng' => $lng,
            'lat' => $lat,
            'ip' => $register_ip,
            'inviteUid' => $inviteUid,
            'from' => 1,
            'package' => $params['package'] ?? '',
            'language' => $params['language'] ?? 'en',
            'network_state' => $params['network_state'] ?? 4,
            'phone_brand' => $params['phone_brand'] ?? '',
            'phone_makers' => $params['phone_makers'] ?? '',
            'system_version' => $params['system_version'] ?? '',
            'version_code' => $params['version_code'] ?? 0,
            'accessToken' => $params['accessToken'] ?? '',// Google登录token或者苹果登录的token、一键登录、阿里云一键登录
            'ydToken' => $params['ydToken'] ?? '',
            'appleId' => $params['appleId'] ?? '',
            'password' => $params['password'] ?? '',
            'uid' => $params['uid'] ?? '',
            'session_id' => $params['session_id'] ?? '',
            'login_from' => $params['login_from'] ?? 'app',
        ];
        $commonParams['channel'] = $this->loginService->getChannel($oaid, $imei, $params['channel'] ?? '');


        $loginResult = $this->loginService->main($account, $code, $type, $commonParams);
        return $this->return($loginResult);

    }


    /**
     * Note - 退出登录
     * DateTime 2024/8/7 16:15
     * User Admin
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function loginOut()
    {
        $user = Context::get('user');
        $rs = $this->loginService->loginOut($user['id']);
        return $this->return($rs);
    }
}
